﻿using BLL;
using MODEL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Loginter.登录用户信息
{
    public partial class UpdateUserInfo : System.Web.UI.Page
    {
        RoleBLL bll = new RoleBLL();
        UserBLL man = new UserBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRoleInfo();
                BindUpdate();
            }
        }
        /// <summary>
        /// 绑定角色用户信息
        /// </summary>
        public void BindRoleInfo()
        {
            DataTable table = bll.GetRoleList();
            drop_RoleID.DataSource = table;
            drop_RoleID.DataTextField = "RoleName";
            drop_RoleID.DataValueField = "RoleID";
            drop_RoleID.DataBind();

            drop_RoleID.Items.Insert(0, new ListItem("--请输入--", "0"));
        }

        protected void btn_update_Click(object sender, EventArgs e)
        {
            //将进行绑定的信息添加到buttonclick事件中运行
            //BindUpdate();
            //姓名
            string name = txt_Name.Text.Trim();
            //性别
            int sex = Convert.ToInt32(rbl_Sex.SelectedValue);
            //角色id
            int roleid = Convert.ToInt32(drop_RoleID.SelectedValue);
            //电话
            string phone = txt_Phone.Text.Trim();
            //邮箱
            string email = txt_Email.Text.Trim();
            //账户
            string account = txt_Accouont.Text.Trim();
            //修改时间
            string time = DateTime.Now.ToShortDateString();
            //用户ID
            int id = Convert.ToInt32(Request.QueryString["userid"]);

            //电话进行格式和非空等数据验证
            if (!Regex.IsMatch(phone, @"^1[3578]\d{9}$"))
            {
                lb_Info.Text = "输入的电话格式不正确！";
                return;
            }
            //邮箱
            if (!Regex.IsMatch(email, @"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$"))
            {
                lb_Info.Text = "输入的电子邮箱格式不正确！";
                return;
            }
            UpdateUserInfoModel model = new UpdateUserInfoModel()
            {
                //id
                UserID = id,
                //姓名
                UserName = name,
                //性别
                Sex = sex,
                //账号
                Account = account,
                //角色id
                FK_RoleID = roleid,
                //电话
                Phone = phone,
                //邮箱
                Email = email,
                //修改时间
                AlterTime = Convert.ToDateTime(time)
            };
            int result = man.Update(model);
            if (result > 0)
            {
                //跳转页面到用户信息
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('修改成功！');location.href='UserInfo.aspx';</script>");
            }
            else
            {
                lb_Info.Text = "修改失败！";
            }
        }

        /// <summary>
        /// 绑定用户信息并修改信息
        /// </summary>
        private void BindUpdate()
        {
            //获取用户信息的id
            int userid = Convert.ToInt32(Request.QueryString["userid"]);
            if (userid == 0)
            {
                Response.Redirect("../系统维护/UserInfoWeiHu.aspx");
            }
            //调用model层的数据
            UpdateUserInfoModel model = man.UpdateUserModelID(userid);
            if (model != null)
            {
                //姓名
                txt_Name.Text = model.UserName;
                //性别
                rbl_Sex.SelectedValue = model.Sex.ToString();
                //角色
                drop_RoleID.SelectedValue = Convert.ToString(model.FK_RoleID);
                //电话
                txt_Phone.Text = model.Phone;
                //邮箱
                txt_Email.Text = model.Email;
                //账户
                txt_Accouont.Text = model.Account;
            }
        }
    }
}